<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 4/26/19
 * Time: 11:12 AM
 */

namespace app\index\model;

use think\Db;
use think\Model;

class DealerModel extends Model
{
    public static function addDealer($data){
        $msg = Db::table("tb_dealer")->where("de_openid",$data["de_openid"])->find();
        $isNull = true;
        if (is_array($msg)){
            $isNull = false;
        }
        if ($isNull){
            $request = Db::table('tb_dealer')->insert($data);
        }else{
            $request = 0;
        }
        return $request;
    }

    public static function login($de_openid){
        $msg = Db::table("tb_dealer")->where("de_openid",$de_openid)->find();
        $isNull = true;
        if (is_array($msg)){
            $isNull = false;
        }
        if ($isNull){
            $msg["code"] = 400;
            $msg["msg"] = "用戶不存在！";
        }else{
            $msg["code"] = 200;
            $msg["msg"] = "登錄成功！";
        }
        return $msg;
    }

    public static function selectAllDealer(){
        $msg = Db::query('select *  from tb_dealer');
        return $msg;
    }

    public static function selectLikeDealer($data){
        $msg = Db::name('tb_dealer')->where('de_name','like',"%".$data."%")->select();
        return $msg;
    }
    public static function updateDealerType($data){
        $msg = Db::table('tb_dealer')->where('de_id', $data["de_id"])->update(['de_type' => $data["de_type"]]);
        return $msg;
    } 
}